<template>
  <page-wrap hideTab back stayStill title="搜索结果">
    <view
      :class="[
        'result',
        from === 'deliveryCompetitivenessService' ? 'padding-bottom' : '',
      ]"
    >
      <SearchBox
        :text="keyword"
        :plain="false"
        textColor="#333333"
        @click.native="handleToSearch"
      />
      <view class="total"
        >共搜索到 <text class="high-light">{{ total }}</text> 结果</view
      >
      <view class="list">
        <SearchItem
          v-for="item in list"
          :from="from"
          :key="item.activityId"
          :item="item"
          :keyword="keyword"
          @click="handleClick(item)"
          @select="handleSelect(item)"
        />
        <u-loadmore
          :status="moreLoading"
          :load-text="loadText"
          v-if="list.length || !loaded"
        />
        <Empty
          v-if="loaded && !list.length"
          :baseImg="emptyImg"
          textColor="#333333"
          text="没有找到您搜索的内容，请换个关键词试试"
          fontSize="28"
          :fontWeight="400"
        />
      </view>
      <view
        class="fixed-bottom"
        v-if="from === 'deliveryCompetitivenessService'"
      >
        <u-button type="primary" shape="circle" @click="handleGoToDelivery"
          >继续交付</u-button
        >
      </view>
    </view>
  </page-wrap>
</template>

<script>
import SearchBox from "../components/SearchBox.vue";
import SearchItem from "../components/SearchItem.vue";
import Empty from "@/components/Empty.vue";

import { getSearchList } from "@/api/modules/library";

export default {
  components: {
    SearchBox,
    SearchItem,
    Empty,
  },
  data() {
    return {
      keyword: "",
      list: [],
      emptyImg: this.$imgBaseSelf + "common/empty.png",
      moreLoading: "loadmore",
      loadText: {
        loadmore: "加载更多",
        loading: "加载中",
        nomore: "已经到底啦~",
      },
      loaded: false,
      pageParams: {
        pageNo: 0,
        pageSize: 20,
      },
      total: 0,
      currentReport: "",
      from: "",
      str: "",
      serviceId: "",
      serviceRecordId: "",
    };
  },
  onLoad(opt) {
    this.keyword = opt.keyword || "";
    this.from = opt.from || "";
    this.str = opt.str || "";
    this.serviceId = opt.serviceId || "";
    this.serviceRecordId = opt.serviceRecordId || "";
    this.getdata();
  },
  onReachBottom() {
    this.getdata(true);
  },
  methods: {
    handleSelect(item) {
      getApp().globalData[this.str] = item;
      this.list.forEach((ele) => {
        if (ele.activityIdCode == item.activityIdCode) {
          ele.checked = true;
          this.currentReport = item;
        } else {
          ele.checked = false;
        }
      });
    },
    handleGoToDelivery() {
      if (!this.currentReport) {
        uni.showToast({ icon: "none", title: "请选择一份推荐的职业报告！" });
        return;
      }
      uni.reLaunch({
        url: `/pagesSub/userCenter/deliveryCompetitivenessService/index?str=${this.str}&serviceId=${this.serviceId}&serviceRecordId=${this.serviceRecordId}`,
      });
    },
    handleToSearch() {
      uni.navigateBack({
        fail: function () {
          uni.navigateTo({
            url: "/pagesSub/library/search/index",
          });
        },
      });
    },
    getdata(isMore) {
      if (this.moreLoading == "loading" || this.moreLoading == "nomore") return;
      const params = {
        keyword: this.keyword,
        pageNo: this.pageParams.pageNo,
        pageSize: this.pageParams.pageSize,
      };
      this.moreLoading = "loading";
      getSearchList(params).then((res) => {
        this.moreLoading = "loadmore";

        if (res.code == 200) {
          let data = res.data || [];
          this.total = res.pageparam.sums;
          data.forEach((element) => {
            element.tagList = element.activityTagName.split(",");
            element.checked = false;
          });
          if (isMore) {
            this.list.push(...data);
          } else {
            this.list = data;
          }
          this.pageParams.pageNo++;
          if (this.pageParams.pageNo > res.pageparam.pages) {
            this.moreLoading = "nomore";
          }
        } else {
          uni.showToast({
            title: res.status_desc,
            icon: "none",
          });
        }
      });
    },
    handleClick(item) {
      if (item.activityTypeCode == "CAREER_CASE") {
        uni.navigateTo({
          url: "/pagesSub/companyCenter/case/detail?id=" + item.activityIdCode,
        });
      } else if (item.activityTypeCode == "CAREER_REPORT") {
        uni.navigateTo({
          url:
            "/pagesSub/companyCenter/report/detail?id=" + item.activityIdCode,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.result {
  position: relative;
  .high-light {
    color: $high-light;
  }
  .total {
    padding: 32rpx 0;
    text-align: center;
    color: #999999;
  }
  .fixed-bottom {
    position: fixed;
    bottom: constant(safe-area-inset-bottom);
    bottom: env(safe-area-inset-bottom);
    width: 100%;
    padding: 24rpx 32rpx;
    z-index: 2;
  }
}
.padding-bottom {
  padding-bottom: 140rpx;
}
</style>
